package com.linko.intelligent.sys.core.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.linko.intelligent.common.util.UserContext;
import com.linko.intelligent.sys.core.mapper.RoleMapper;
import com.linko.intelligent.sys.core.service.RoleService;
import com.linko.intelligent.sys.core.service.UserService;
import com.linko.intelligent.sys.pojo.dto.RoleDto;
import com.linko.intelligent.sys.pojo.po.Role;
import com.linko.intelligent.sys.pojo.query.RoleQuery;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @Author yuan
 * @create 2024/3/15 11:42
 */
@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements RoleService {


    @Autowired
    private RoleMapper roleMapper;

    @Autowired
    private UserService userService;

    @Override
    public Long save(RoleDto dto) {
        Role po = new Role();
        BeanUtils.copyProperties(dto, po);
        po.setCreatorId(UserContext.getUserId());
        po.setCreateTime(new Date());
        po.setDel(0L);
        save(po);
        return po.getId();
    }

    @Override
    public Boolean delete(Long id) {
        Role po = new Role();
        po.setId(id);
        po.setDel(1L);
        return updateById(po);
    }

    @Override
    public Boolean update(Long id, RoleDto dto) {
        Role po = new Role();
        BeanUtils.copyProperties(dto, po);
        po.setId(id);
        return updateById(po);
    }

    @Override
    public Role getDetail(Long id) {
        Role po = getById(id);
        return po;
    }

    @Override
    public List<Role> queryList(RoleQuery query) {
        List<Role> list = roleMapper.selectLs(query);
        return list;
    }


    @Override
    public PageInfo<Role> queryPage(RoleQuery query) {
        PageInfo<Role> pageInfo = PageHelper.startPage(query.getPn(), query.getPs()).doSelectPageInfo(
                () -> queryList(query)
        );
        return pageInfo;
    }

}
